
import { Context } from 'koa';
import { validateDto } from '@/utils/validateDto';
import { 
    CreateDictDto, 
    UpdateDictDto, 
    QueryDictDto, 
    DeleteDictDto, 
    SelectOneDictDto,
    //SortDictDto 
} from '@/dto/dict.dto';
import dictService from '@/services/dict.service';

class DictController {
    async selectList(ctx: Context) {
        const reqData = ctx.request.body as QueryDictDto;
        const errors = await validateDto(QueryDictDto, reqData);
        if (errors) {
            ctx.body = errors;
            return
        }
        let resData = await dictService.selectList(reqData)
        ctx.body = {
            code: 200,
            message: "列表查询成功",
            data:resData
        };
    }
    async selectOne(ctx: Context) {
        const reqData = ctx.request.body as SelectOneDictDto;
        let resData = await dictService.selectOne(reqData)        
        ctx.body = {
            code: 200,
            message:"详情查询成功",
            data: resData,

        };
    }

    async add(ctx: Context) {
        const userId = ctx.state.userId as number;
        const reqData = ctx.request.body as CreateDictDto;
        //reqData.userId = reqData.userId || userId; //暂时注释
        const errors = await validateDto(CreateDictDto, reqData);
        if (errors) {
            ctx.body = errors;
            return
        }
        const resData = await dictService.add(reqData);        
        ctx.body = {
        code: 200,
        message:"添加成功",
        data:{id:resData.id},
        };

    }

    async update(ctx: Context) {
        const reqData = <UpdateDictDto>ctx.request.body
        const errors = await validateDto(UpdateDictDto, reqData);
        if (errors) {
            ctx.body = errors;
            return
        }
        const resData = await dictService.update(reqData);
        ctx.body = {
            code: 200,
            message:"更新成功",
            data: {},
        };

    }
    async delete(ctx: Context) {
        const reqData = ctx.request.body as DeleteDictDto;
        const errors = await validateDto(DeleteDictDto, reqData);
        if (errors) {
            ctx.body = errors;
            return
        }
        const resData = await dictService.delete(reqData);
        ctx.body = {
            code: 200,
            message:"删除成功",
            data: {},
        };

    }
    /**
    async sort(ctx: Context) {
        const reqData = ctx.request.body as SortDictDto;
        const errors = await validateDto(SortDictDto, reqData);
        if (errors) {
            ctx.body = errors;
            return
        }
        const resData = await dictService.sort(reqData);    
        ctx.body = {
            code: 200,
            message:"排序成功",
            data: {},
        };
    
    }*/

}

export default new DictController();